﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HCFramework.API.LINQ;
using Telerik.Web.UI;

namespace HCFramework.Product.CategoryManage
{
    public partial class ChooseProductCategory : System.Web.UI.UserControl
    {
        public RadTreeView Tree
        {
            get
            {
                return RadComboBox1.Items[0].FindControl("RadTreeView1") as RadTreeView;
            }
        }

        public int? SelectedCategoryID
        {
            get
            {
                if (Tree.SelectedNode == null) return null;
                return int.Parse(Tree.SelectedValue);
            }
            set
            {
                Tree.ClearSelectedNodes();
                if (value.HasValue)
                {
                    RadTreeNode node = Tree.FindNodeByValue(value.Value.ToString());
                    node.Selected = true;
                    node.ExpandParentNodes();
                    SetText(node.Text);
                }
            }
        }

        public void SetText(string text)
        {
            RadComboBox1.Items[0].Text = text;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
            }
            SelectedCategoryID = SelectedCategoryID;
        }

        public void LoadTreeData()
        {
            HCDataContext db = new HCDataContext();
            Tree.DataSource = db.ProductCategories.OrderBy(ik => ik.OrderDisplay);
            Tree.DataBind();

            foreach (RadTreeNode leafNode in Tree.GetAllNodes().Where(ik => ik.Nodes.Count == 0))
            {
                leafNode.CssClass = "leafNode";
            }
        }

        public bool OnlySelectLeafNode { get; set; }
    }
}